package com.xqx.http;
import	java.util.Enumeration;

import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.*;

/**
 * @author：xingquanxiang createTime：2019/10/20 8:57
 * description:
 */
public class HttpClientTest {
    public static void main(String[] args) throws IOException {
        //创建客户端编译器
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        // httpClientBuilder.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36");
        //生成客户端
        HttpClient client = httpClientBuilder.build();
        //设置请求服务器参数
        HttpGet httpRequest = new HttpGet("https://www.baidu.com/");
        // httpRequest.setHeader("Host","www.baidu.com");
        // httpRequest.setHeader("Connection", "keep-alive");
        // httpRequest.setHeader("Pragma", "no-cache");
        // httpRequest.setHeader("Cache-Control", "no-cache");
        // httpRequest.setHeader("Upgrade-Insecure-Requests", "１");
        // httpRequest.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36");
        // httpRequest.setHeader("Sec-Fetch-Mode", "navigate");
        // httpRequest.setHeader("Sec-Fetch-User", "?1");
        // httpRequest.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
        // httpRequest.setHeader("", "");
        // httpRequest.setHeader("Sec-Fetch-Site", "gzip, deflate, br");
        // httpRequest.setHeader("Accept-Language", "en-GB,en-US;q=0.9,en;q=0.8,zh-CN;q=0.7,zh;q=0.6");
        // httpRequest.setHeader("Cookie", "BAIDUID=F02E4AD64E5F0C77029A99F73BD21429:FG=1; PSTM=1556882148; BIDUPSID=D021E76B09CE00C300BC050FF5EEB722; BD_UPN=12314753; BDUSS=pPYnRmS2dwWVVRbHRHVW04SFpBcExseDRObDItS0NMYVBreVdGRDBVVDh-VUpkRVFBQUFBJCQAAAAAAAAAAAEAAADnADhezt7O0s7ezsoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPxwG138cBtdNG; H_WISE_SIDS=126124_133105_129118_128064_131887_126065_133676_120179_133972_133016_132909_133042_131247_132439_130762_132378_131518_118893_118864_118846_118824_118801_107317_133159_132783_134394_133351_129651_134433_128967_132538_133835_133472_131906_133838_133847_132552_133288_134320_132543_132494_129643_131423_132412_134346_133996_133916_110085_132948_134152_127969_131298_130598_127416_133727_134150_133176_134353; BD_HOME=1; H_PS_PSSID=1439_21081_20698_29567_29221; SL_GWPT_Show_Hide_tmp=1; SL_wptGlobTipTmp=1; delPer=0; BD_CK_SAM=1; PSINO=7; B64_BOT=1; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_645EC=1ebc2rmz6DeyxUzynPrYgxYxnRvFusotDeNstN3LvhYIFjwPA3zZD6f8IYw");
        System.out.println("httpRequest = " + httpRequest);
        for (Header header : httpRequest.getAllHeaders()) {
            System.out.println(header.getName()+":"+header.getValue());
        }
        System.out.println("------------------------");
        //执行请求  服务器响应
        HttpResponse httpResponse = client.execute(httpRequest);

        StatusLine statusLine = httpResponse.getStatusLine();
        System.out.println("statusLine.toString() = " + statusLine.toString());

        HttpEntity entity = httpResponse.getEntity();

        System.out.println("------------------------");
        System.out.println("httpResponse = " + httpResponse);
        Header contentType = entity.getContentType();
        HeaderElement[] elements = contentType.getElements();
        for (HeaderElement element : elements) {
            System.out.println(element.getName()+":"+element.getValue());
        }
        System.out.println("------------------------");

/*        InputStream content = entity.getContent();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(content));
        while (bufferedReader.ready()){
            System.out.println(bufferedReader.readLine());
        }
        bufferedReader.close();
        */
        OutputStream outputStream = new FileOutputStream(new File("G:\\DarkHorseCode\\ssm\\Spring\\crawle_test\\src\\main\\java\\com\\xqx\\http\\content.html"));
        entity.writeTo(outputStream);

        for (Header header : httpResponse.getAllHeaders()) {
            System.out.println(header.getName()+":"+header.getValue());
        }

    }
}
